package com.wangjie.string;

import java.nio.charset.Charset;
import java.util.Arrays;

/**
 * 字符串 三个特点
 * 1 字符串的内容永不可变
 * 2 由于字符串的内容永不可变 所以字符串可以共享使用
 * 3 字符串效果上相当于char[] 字符数组 但是 底层原理是byte[]字节数组
 */
public class StringDemo01 {
    public static void main(String[] args) {
        /*一个中文字符等于两个字节 此处不可以使用中文*/
        byte [] bytes = {'a','b','c'};
        String str = new String (bytes);
        System.out.println(str);

        byte [] bytes2 = {'a','b','c'};
        String str2 = new String (bytes2, Charset.forName("UTF-8"));
        System.out.println(str);

        char [] chars = {'a','中','国'};
        String str3 = new String (chars);
        System.out.println(str3);

        char [] chars2 = {'a','中','国'};
        String str4 = new String (chars2,1,2);
        System.out.println(str4);

        String str5 = new String("hello world");
        System.out.println(str5);

        String str6 = new String("kobebrant");
        System.out.println(str5.compareTo(str6));//其实比较的是首字母

        System.out.println(str5.compareToIgnoreCase(str6));

        System.out.println(str6.charAt(1));

        System.out.println(str5.concat(str6));

        System.out.println(str6.contains("ant"));

        System.out.println(Arrays.toString(str6.getBytes()) );

        System.out.println(str6.indexOf("ob"));
        System.out.println(str6.indexOf("an",8));
        String [] arr = {"hello","world"};
        System.out.println(String.join("-",arr));

        System.out.println(Arrays.toString(str6.split("an")) );
        System.out.println(Arrays.toString(str6.toCharArray()));
        CharSequence charSequence = str6.subSequence(3,5);
        System.out.println(charSequence.toString());

        System.out.println(str6.substring(3));
        System.out.println(str6.substring(3,6));

    }
}
